<template>
  <van-tabbar
    v-model="active"
    active-color="#ff6700"
    inactive-color="#999"
    @change="changeTab(active)"
  >
    <van-tabbar-item
      :icon="
        active === 0
          ? '/images/iconfont/icon-home-selected.png'
          : '/images/iconfont/icon-home.png'
      "
      >主页</van-tabbar-item
    >
    <van-tabbar-item
      :icon="
        active === 1
          ? '/images/iconfont/icon-category-selected.png'
          : '/images/iconfont/icon-category.png'
      "
      >分类</van-tabbar-item
    >
    <van-tabbar-item
      :icon="
        active === 2
          ? '/images/iconfont/icon-cart-selected.png'
          : '/images/iconfont/icon-cart.png'
      "
      >购物车</van-tabbar-item
    >
    <van-tabbar-item
      :icon="
        active === 3
          ? '/images/iconfont/icon-user-selected.png'
          : '/images/iconfont/icon-user.png'
      "
      >我的</van-tabbar-item
    >
  </van-tabbar>
</template>

<script>
export default {
  name: "Footer",
  data() {
    return {
      active: 0, //存的是索引值
    };
  },
  computed: {
    changeRouter() {
      return this.$route.path;
    },
  },
  watch: {
    changeRouter: {
      handler() {
        immediately: true;
        this.change();
      },
    },
  },
  methods: {
    changeTab(active) {
      if (active === 0) {
        this.$router.push("/home");
      } else if (active === 1) {
        this.$router.push("/category");
      } else if (active === 2) {
        this.$router.push("/cart");
      } else if (active === 3) {
        this.$router.push("/personal");
      }
    },
    change() {
      let { path } = this.$route;
      if (path === "/home") {
        this.active = 0;
      } else if (path === "/category") {
        this.active = 1;
      } else if (path === "/cart") {
        this.active = 2;
      } else if (path === "/personal") {
        this.active = 3;
      }
    },
  },
  mounted() {
    // this.changeTab(this.active);
    // this.active = 0;
  },
};
</script>

<style lang="less" scoped>
</style>